package com.cykj.product.service.impl;
import com.cykj.common.core.utils.StringUtils;
import com.cykj.product.domain.BsProductCategory;
import com.cykj.product.domain.BsProductInformation;
import com.cykj.product.mapper.BsProductInformationMapper;
import com.cykj.product.service.BsProductInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class BsProductInformationServiceImpl implements BsProductInformationService {

    @Autowired
    private BsProductInformationMapper bsProductInformationMapper;
    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public BsProductInformation selectBsProductInformationById(Long id)
    {
        return bsProductInformationMapper.selectBsProductInformationById(id);
    }
    @Override
    public List<BsProductInformation> selectProductListWithCategory(BsProductInformation bsProductInformation) {
        return bsProductInformationMapper.selectProductListWithCategory(bsProductInformation);
    }
    // 下架状态值（例如2表示下架）
    private static final Long OFF_SHELF_STATUS = 0L;

    @Override
    public int takeOffProduct(Long id) {

        return bsProductInformationMapper.updateProductStatus(id, OFF_SHELF_STATUS);
    }

    @Override
    public int batchTakeOffProduct(List<Long> ids) {
        return bsProductInformationMapper.batchUpdateProductStatus(ids, OFF_SHELF_STATUS);
    }
    /**
     * 新增商品信息
     *
     * @param bsProductInformation 商品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBsProductInformation(BsProductInformation bsProductInformation)
    {
        int rows = bsProductInformationMapper.insertBsProductInformation(bsProductInformation);
        insertBsProductCategory(bsProductInformation);
        return rows;
    }

    /**
     * 修改商品信息
     *
     * @param bsProductInformation 商品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBsProductInformation(BsProductInformation bsProductInformation)
    {
        return bsProductInformationMapper.updateBsProductInformation(bsProductInformation);
    }
    /**
     * 新增商品类目信息
     *
     * @param bsProductInformation 商品信息对象
     */
    public void insertBsProductCategory(BsProductInformation bsProductInformation)
    {
        List<BsProductCategory> bsProductCategoryList = bsProductInformation.getBsProductCategoryList();
        Long id = bsProductInformation.getId();
        if (StringUtils.isNotNull(bsProductCategoryList))
        {
            List<BsProductCategory> list = new ArrayList<BsProductCategory>();
            for (BsProductCategory bsProductCategory : bsProductCategoryList)
            {
                bsProductCategory.setId(id);
                list.add(bsProductCategory);
            }
            if (list.size() > 0)
            {
                bsProductInformationMapper.batchBsProductCategory(list);
            }
        }
    }

    @Override
    public List<BsProductCategory> selectCategoryByShopId(Long shopId) {
        if (shopId == null || shopId <= 0) {
            return new ArrayList<>();
        }
        return bsProductInformationMapper.selectCategoryByShopId(shopId);
    }
}